package com._5704.website.service.ServiceImpl;

import com._5704.website.entity.Project;
import com._5704.website.entity.User;
import com._5704.website.exception.WebsiteException;
import com._5704.website.exception.WebsiteExceptionEnum;
import com._5704.website.form.ProjectForm;
import com._5704.website.mapper.ProjectMapper;
import com._5704.website.service.IProjectService;
import com._5704.website.util.FileUtil;
import com._5704.website.util.PictureUtil;
import com._5704.website.vo.projectVO.ProjectAndMembersVO;
import com._5704.website.vo.projectVO.ProjectVO;
import com._5704.website.vo.userVO.UserVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Yiyoung
 * @since 2021-07-25
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {
    @Autowired
    private  ProjectMapper projectMapper;
    @Value("${download.filePath}")
    private  String path;

    @Resource
    private FileUtil fileUtil;
    /**
     * 创建项目
     *  @param projectForm 项目表单
     * @param userId      用户id
     * @return
     */
    @Override
    public int createProject(ProjectForm projectForm, String userId) {
        Project project = new Project()
                .setName(projectForm.getName())
                .setIntroduce(projectForm.getIntroduce())
                .setLeadUserId(userId)
                .setInstructor(projectForm.getInstructor())
                .setUserNum(projectForm.getUserNum())
                .setCreateTime(new Date())
                .setPicture("project/0.png")
                .setType(projectForm.getType());
        // 插入并获得id
        projectMapper.insert(project);
        return project.getId();

    }

    /**
     * 检查成员是否满了
     *
     * @param id 项目id
     * @return
     */
    @Override
    public boolean isMemberFull(Integer id) {
        // 获得项目成员
        List<User> members = projectMapper.getMembers(id);
        // 获得项目
        Project project = projectMapper.selectById(id);

        return project.getUserNum() <= members.size();

    }

    /**
     * 设置成员
     *
     * @param userId 用户id
     * @param id     项目id
     */
    @Override
    public void setMember(String userId, int id) {
        // 设置成员
        projectMapper.setMember(userId,id);
    }

    /**
     * 获得多个项目
     *
     * @param page 页数
     * @param num  数量
     * @return 项目list
     */
    @Override
    public List<Project> getProjects(int page, int num) {
        List<Project> projectList = projectMapper.getProjects(page,num);
        return projectList;
    }

    /**
     * 获得项目
     *
     * @param id 项目id
     * @return 项目
     */
    @Override
    public Project getProject(Integer id) {
        Project project = projectMapper.selectById(id);
        if (project == null){
            throw new WebsiteException(WebsiteExceptionEnum.PROJECT_NOT_FOUND);
        }
        return project;
    }

    /**
     * 获得VO
     *
     * @param projectVO 项目VO
     * @param userVOList 成员VO list
     * @return VO
     */
    @Override
    public ProjectAndMembersVO getProjectAndMembersVO(ProjectVO projectVO, List<UserVO> userVOList) {
        return new ProjectAndMembersVO()
                .setProjectVO(projectVO)
                .setUserVOList(userVOList);
    }

    /**
     * 获得参加项目的用户
     *
     * @param id 项目id
     * @return 用户list
     */
    @Override
    public List<User> getMembers(Integer id) {
        return projectMapper.getMembers(id);
    }

    /**
     * 获得VO
     *
     * @param project 项目
     * @param leader 创建人
     * @return VO
     */
    @Override
    public ProjectVO getProjectVO(Project project, UserVO leader) {
       return new ProjectVO()
                .setName(project.getName())
                .setCreateTime(project.getCreateTime())
                .setIntroduce(project.getIntroduce())
                .setInstructor(project.getInstructor())
                .setLeader(leader)
                .setId(project.getId())
                .setType(project.getType())
               .setUserNum(project.getUserNum())
               .setPicture(project.getPicture());

    }

    /**
     * 检查用户是否为项目创造者
     *
     * @param id     项目id
     * @param userId 用户id
     * @return
     */
    @Override
    public boolean isCreator(Integer id, String userId) {
        return projectMapper.selectById(id).getLeadUserId().equals(userId);
    }

    /**
     * 取消成员
     *
     * @param userId 用户id
     * @param id     项目id
     */
    @Override
    public void cancelMember(String userId, Integer id) {
        projectMapper.cancelMember(userId, id);
    }

    /**
     * 是否为团队成员
     *  @param userId 用户id
     * @param id     项目id
     * @return
     */
    @Override
    public boolean isMember(String userId, Integer id) {
        //检查是否已经参与
        return projectMapper.getMember(userId, id) != null || projectMapper.selectById(id).getLeadUserId().equals(userId);
    }

    /**
     * 删除
     *
     * @param project 项目id
     */
    @Override
    public void delete(Project project) {
        Integer projectId = project.getId();
        String picture = project.getPicture();
        // 清除参与人员
        List<User> members = projectMapper.getMembers(projectId);
        members.forEach(e->{
            projectMapper.cancelMember(e.getUserId(), projectId);
        });
        if (!picture.equals("project/0.png")){
            fileUtil.delete(picture);
        }
        projectMapper.deleteById(project);
    }

    /**
     * 设置更新
     *
     * @param project     项目
     * @param projectForm 项目表单
     */
    @Override
    public void setModifyData(Project project, ProjectForm projectForm) {


        project.setIntroduce(projectForm.getIntroduce())
                .setId(projectForm.getId())
                .setInstructor(projectForm.getInstructor())
                .setName(projectForm.getName())
                .setType(projectForm.getType())
                .setUserNum(projectForm.getUserNum());
    }

    /**
     * 更新
     *
     * @param project 项目
     */
    @Override
    public void updateProject(Project project) {
        projectMapper.updateById(project);
    }


    /**
     * 获得项目总数
     *
     * @return
     */
    @Override
    public int getNum() {
        return projectMapper.getNum();
    }


    /**
     * 获得自己的项目
     *
     * @param userId 用户id
     * @param page
     * @param num
     * @return
     */
    @Override
    public List<Project> getMyProject(String userId, int page, int num) {
        return projectMapper.getMyProject(userId,page,num);
    }

    /**
     * 获得参加的项目
     *
     * @param userId 用户id
     * @param page
     * @param num
     * @return
     */
    @Override
    public List<Project> getPartProject(String userId, int page, int num) {
        return projectMapper.getPartProject(userId,page,num);
    }

    /**
     * 根据名字获得项目
     *
     * @param projectName 名字
     * @param page
     * @param num
     * @return
     */
    @Override
    public List<Project> getProjectByName(String projectName, int page, int num) {
        return projectMapper.getProjectByName(projectName,page,num);
    }

    /**
     * 获得数量
     *
     * @param userId
     * @return
     */
    public int getMineNum(String userId) {
        return projectMapper.getMineNum(userId);
    }

    /**
     * 获得数量
     *
     * @param userId
     * @return
     */
    @Override
    public int getPartNum(String userId) {
        return projectMapper.getPartNum(userId);
    }


    /**
     * 获得数量
     *
     * @param isSearch
     * @return
     */
    @Override
    public int getNum(String isSearch) {
        return projectMapper.getNumSea(isSearch);
    }

    /**
     * 更新图片
     *
     * @param file 文件
     * @param id   活动id
     */
    @Override
    public void uploadPic(MultipartFile file, Integer id) {
        if (file.isEmpty()){
            throw new WebsiteException(WebsiteExceptionEnum.FILE_ERROR);
        }
        // 设置数据库文件名
        String DataBaseFileName = "project/"+id + ".png";
        // 工具类上传
        fileUtil.upload(file,DataBaseFileName);
        // 修改数据库
        projectMapper.updatePic(DataBaseFileName,id);
    }
}
